//
//  ModelInfo.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-4-22.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//  舰船模型文件的信息类，纪录了模型的宽高，及8个攻击和防御炮位

#import "ShipModelInfo.h"
#import "Variable.h"

@implementation ShipModelInfo

@synthesize modelId = _modelId;
@synthesize width = _width;
@synthesize height = _height;
@synthesize anchorPoint = _anchorPoint;

@synthesize attackArtillery1 = _attackArtillery1;
@synthesize attackArtillery2 = _attackArtillery2;
@synthesize attackArtillery3 = _attackArtillery3;
@synthesize attackArtillery4 = _attackArtillery4;
@synthesize attackArtillery5 = _attackArtillery5;
@synthesize attackArtillery6 = _attackArtillery6;
@synthesize attackArtillery7 = _attackArtillery7;
@synthesize attackArtillery8 = _attackArtillery8;

@synthesize defenseArtillery1 = _defenseArtillery1;
@synthesize defenseArtillery2 = _defenseArtillery2;
@synthesize defenseArtillery3 = _defenseArtillery3;
@synthesize defenseArtillery4 = _defenseArtillery4;
@synthesize defenseArtillery5 = _defenseArtillery5;
@synthesize defenseArtillery6 = _defenseArtillery6;
@synthesize defenseArtillery7 = _defenseArtillery7;
@synthesize defenseArtillery8 = _defenseArtillery8;

+(id) modelWithTbxml:(TBXMLElement *)tbxml
{
    return [[[self alloc] initWithTbxml:tbxml] autorelease]; 
}


-(id) initWithTbxml:(TBXMLElement *)tbxml
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        //读取模型的信息，在属性里面
        NSString * x;
        NSString * y;
        NSString * maxAngle;
        NSString * minAngle;
        NSArray *array;
        
        //模型的id
        self.modelId = [[TBXML valueOfAttributeNamed:@"id" forElement:tbxml] intValue];
        if(showXMLLog == YES) CCLOG(@"modelId:%d", self.modelId);
        
        //模型的宽
        self.width = [[TBXML valueOfAttributeNamed:@"width" forElement:tbxml] floatValue];
        if(showXMLLog == YES) CCLOG(@"width:%f", self.width);
        
        //模型的高
        self.height = [[TBXML valueOfAttributeNamed:@"height" forElement:tbxml] floatValue];
        if(showXMLLog == YES) CCLOG(@"height:%f", self.height);
        
        //重心点，锚点
        NSString * anchorPoint = [TBXML valueOfAttributeNamed:@"anchorPoint" forElement:tbxml];
        if([anchorPoint isEqualToString: @"0"] == NO)//非0保护
        {
            array = [anchorPoint componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            self.anchorPoint = ccp([x floatValue] / self.width, (1 - [y floatValue] / self.height));
            if(showXMLLog == YES) CCLOG(@"anchorPoint:%f,%f", [x floatValue], [y floatValue]);
        }
        else
        {
            self.anchorPoint = CGPointZero;
        }
        
        
        //攻击炮位1
        NSString * attackArtillery1 = [TBXML valueOfAttributeNamed:@"attackArtillery1" forElement:tbxml];
        if([attackArtillery1 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery1 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery1 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery1:%f,%f,%f,%f", [x floatValue], self.height - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery1 = CGRectZero;
        }
        
        //攻击炮位2
        NSString * attackArtillery2 = [TBXML valueOfAttributeNamed:@"attackArtillery2" forElement:tbxml];
        if([attackArtillery2 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery2 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery2 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery2:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery2 = CGRectZero;
        }
        
        //攻击炮位3
        NSString * attackArtillery3 = [TBXML valueOfAttributeNamed:@"attackArtillery3" forElement:tbxml];
        if([attackArtillery3 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery3 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery3 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery3:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery3 = CGRectZero;
        }
        
        //攻击炮位4
        NSString * attackArtillery4 = [TBXML valueOfAttributeNamed:@"attackArtillery4" forElement:tbxml];
        if([attackArtillery4 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery4 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery4 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery4:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery4 = CGRectZero;
        }
        
        //攻击炮位5
        NSString * attackArtillery5 = [TBXML valueOfAttributeNamed:@"attackArtillery5" forElement:tbxml];
        if([attackArtillery5 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery5 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery5 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery5:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery5 = CGRectZero;
        }
        
        //攻击炮位6
        NSString * attackArtillery6 = [TBXML valueOfAttributeNamed:@"attackArtillery6" forElement:tbxml];
        if([attackArtillery6 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery6 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery6 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery6:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery6 = CGRectZero;
        }
        
        //攻击炮位7
        NSString * attackArtillery7 = [TBXML valueOfAttributeNamed:@"attackArtillery7" forElement:tbxml];
        if([attackArtillery7 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery7 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery7 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery7:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery7 = CGRectZero;
        }
        
        //攻击炮位8
        NSString * attackArtillery8 = [TBXML valueOfAttributeNamed:@"attackArtillery8" forElement:tbxml];
        if([attackArtillery8 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [attackArtillery8 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.attackArtillery8 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"attackArtillery8:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.attackArtillery8 = CGRectZero;
        }
        
        //防御炮位1
        NSString * defenseArtillery1 = [TBXML valueOfAttributeNamed:@"defenseArtillery1" forElement:tbxml];
        if([defenseArtillery1 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery1 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery1 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery1:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery1 = CGRectZero;
        }
        
        //防御炮位2
        NSString * defenseArtillery2 = [TBXML valueOfAttributeNamed:@"defenseArtillery2" forElement:tbxml];
        if([defenseArtillery2 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery2 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery2 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery2:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery2 = CGRectZero;
        }
        
        //防御炮位3
        NSString * defenseArtillery3 = [TBXML valueOfAttributeNamed:@"defenseArtillery3" forElement:tbxml];
        if([defenseArtillery3 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery3 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery3 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery3:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery3 = CGRectZero;
        }
        
        //防御炮位4
        NSString * defenseArtillery4 = [TBXML valueOfAttributeNamed:@"defenseArtillery1" forElement:tbxml];
        if([defenseArtillery4 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery4 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery4 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery4:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery4 = CGRectZero;
        }
        
        //防御炮位5
        NSString * defenseArtillery5 = [TBXML valueOfAttributeNamed:@"defenseArtillery5" forElement:tbxml];
        if([defenseArtillery5 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery5 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery5 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery5:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery5 = CGRectZero;
        }
        
        //防御炮位6
        NSString * defenseArtillery6 = [TBXML valueOfAttributeNamed:@"defenseArtillery6" forElement:tbxml];
        if([defenseArtillery6 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery6 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery6 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery6:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery6 = CGRectZero;
        }
        
        //防御炮位7
        NSString * defenseArtillery7 = [TBXML valueOfAttributeNamed:@"defenseArtillery7" forElement:tbxml];
        if([defenseArtillery7 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery7 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery7 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery7:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery7 = CGRectZero;
        }
        
        //防御炮位8
        NSString * defenseArtillery8 = [TBXML valueOfAttributeNamed:@"defenseArtillery8" forElement:tbxml];
        if([defenseArtillery8 isEqualToString: @"0"] == NO)//非0保护
        {
            array = [defenseArtillery8 componentsSeparatedByString:@","]; 
            x = [array objectAtIndex:0];
            y = [array objectAtIndex:1];
            minAngle = [array objectAtIndex:2];
            maxAngle = [array objectAtIndex:3];
            self.defenseArtillery8 = CGRectMake(-self.width * self.anchorPoint.x + [x floatValue], self.height * (1 -self.anchorPoint.y) - [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
            if(showXMLLog == YES) CCLOG(@"defenseArtillery8:%f,%f,%f,%f", [x floatValue], [y floatValue], [minAngle floatValue], [maxAngle floatValue]);
        }
        else
        {
            self.defenseArtillery8 = CGRectZero;
        }
    }
    
    return self;
}

/**
	根据武器类型和武器的位置，返回武器的位置和旋转的最大和最小角度
	@param weaponType 武器类型
	@param index 武器的炮位
	@returns 武器的炮位坐标和允许旋转的最大最小角度
 */
-(CGRect) getPosAndAngle:(WeaponType)weaponType index:(int)index
{
    CGRect result;
    
    if(weaponType == WeaponAttack)
    {
        switch (index) 
        {
            case 1:
            {
                result = self.attackArtillery1;
                break;
            }
            case 2:
            {
                result = self.attackArtillery2;
                break;
            }
            case 3:
            {
                result = self.attackArtillery3;
                break;
            }
            case 4:
            {
                result = self.attackArtillery4;
                break;
            }
            case 5:
            {
                result = self.attackArtillery5;
                break;
            }
            case 6:
            {
                result = self.attackArtillery6;
                break;
            }
            case 7:
            {
                result = self.attackArtillery7;
                break;
            }
            case 8:
            {
                result = self.attackArtillery8;
                break;
            }
            default:
                break;
        }
    }
    else
    {
        switch (index) 
        {
            case 1:
            {
                result = self.defenseArtillery1;
                break;
            }
            case 2:
            {
                result = self.defenseArtillery2;
                break;
            }
            case 3:
            {
                result = self.defenseArtillery3;
                break;
            }
            case 4:
            {
                result = self.defenseArtillery4;
                break;
            }
            case 5:
            {
                result = self.defenseArtillery5;
                break;
            }
            case 6:
            {
                result = self.defenseArtillery6;
                break;
            }
            case 7:
            {
                result = self.defenseArtillery7;
                break;
            }
            case 8:
            {
                result = self.defenseArtillery8;
                break;
            }
            default:
                break;
        } 
    }
    
    return result;
}

@end
